<?php
if (!defined('MVC_ROOT_DIR'))  die('Hacking attempt');
class User
{
	public $db;
	public function __construct(){
		$this->db=new Mysql();
		Files::loadPlugin('page');
	}
	public function getBasicUserByEmail($email){
	    if ($email){
		    $sql = "SELECT * FROM user WHERE `email` = '$email'";		
		    return $this->db->getRow($sql);
	    }else {
	        return  false;
	    }
	}
	public function getBasicUserByPhone($phone){
		if ($phone){
			$sql = "SELECT * FROM user WHERE `phone` = '$phone'";			
			return $this->db->getRow($sql);
		}else {
			return false;
		}
	}
	public function getBasicUserByUsername($username){
		if ($username){
			$sql = "SELECT * FROM user WHERE `username` = '$username'";
			return $this->db->getRow($sql);
		}else {
			return false;
		}
	}
	public function getBasicUserByCode($code){
		if ($code){
			$sql = "SELECT * FROM user WHERE `code` = '$code'";
			return $this->db->getRow($sql);
		}else {
			return false;
		}
	}
	public function insertUser($data){
		if ($data){
			return $this->db->insert('user', $data,true);
		}else {
			return false;
		}
	}
	public function getBasicUserByUid($uid){
		if ($uid){
			$sql = "SELECT * FROM user WHERE `uid` = '$uid'";
			return $this->db->getRow($sql);
		}else {
			return false;
		}
	}
	public function getDataUser($uid){
	    if ($uid){
			$sql = "SELECT * FROM user_data WHERE `uid` = '$uid'";
			$rs = $this->db->getRow($sql);
			if (!$rs){
			    $rs = array('uid'=>$uid,'exp'=>0,'point'=>0,'totalpoint'=>0,'totalexpend'=>0,'publish'=>0);
			}
			return $rs;
		}else {
			return false;
		}
	}
	public function setLogoin($uid){
		if ($uid){
			$_SESSION['islogin']=1;
			$binfo = $this->getBasicUserByUid($uid);
			$dinfo = $this->getDataUser($uid);
			
			unset($dinfo['uid']);
			$_SESSION['user']=array_merge($binfo,$dinfo);
			
		}
	}
	public function saveBasicinfo($data,$uid){
	    if ($data and $uid){
	        return $this->db->updateData('user', $data, " WHERE uid = '$uid'");
	    }
	    return false;
	    
	}
	
	public static function getPublishTypeName($type){
	    switch ($type){
	        case '1':
	            return '广告链接';
	        case '2':
	            return '软件激活码';
	        case '3':
	            return '软件源码';
	        case '4':
	            return '创意想法';
	        case '0':
	            return '其它';      
	    }
	    return '其它';
	}
	public static function getStatusName($status,$color=null){
	    switch ($status){
	        case '1':
	            if ($color){
	                return '<span style="color:green">审核通过</span>';
	            }else {
	                return '审核通过';
	            }
	        case '2':
	            if ($color){
	                return '<span style="color:#bbb">无效过期</span>';
	            }else {
	                return '无效过期';
	            }
	        case '0':
	            if ($color){
	                return '<span style="color:brown">待审核</span>';
	            }else {
	                return '待审核';
	            }
	              
	    }
	    return '待审核';
	}
	public function getUserPublish($uid){
	    $sql = "SELECT * FROM user_publish WHERE uid = '$uid' ";
		//设置分页信息
		$page = new Page ();
		$page->uriStyle = '?';
		$page->eachNums=2;
		$list = $page->getPage ( $sql, 0 );
		$html = $page->htmlPage ();
// 		echo $html;
		//返回搜索结果和分页信息
		return array ('list' => $list, 'html' => $html, 'total' => $page->allNums, 'eachNums' => $page->eachNums );
	}
	public function getUserPublishInfo($id,$uid){
	    $sql = "SELECT * FROM user_publish WHERE id='$id' AND uid = '$uid' ";
	    return $this->db->getRow($sql);
	}
	public static  function getUserLevel($exp){
	    $level  =floor($exp/100)+1;
	    return $level;
	}
	public static function getNextExp($exp){
	    $level = self::getUserLevel($exp);
	    $nextexp =($level)*100;
	    return $nextexp;
	}
	public function getLottery($uid,$type=-1,$today=false,$page=true){
		if ($today==true){
			$stime = strtotime(date("Y-m-d")." 00:00:00");
			$etime = strtotime(date("Y-m-d")." 23:59:59");
		}else {
			$stime = strtotime(Http::get('stime')." 00:00:00");
			$etime = strtotime(Http::get('etime')." 23:59:59");
		}
		$sql = "SELECT * FROM user_award WHERE uid = $uid ";
		if($type!=-1){
			$sql = $sql. "  AND `type` = $type ";
		}
		$sql = $sql." AND timeline>=$stime and  timeline<=$etime ";
		if ($page){
			$page = new Page ();
			$page->uriStyle = '/';
			$page->eachNums=15;
			$list = $page->getPage ( $sql, 0 );
			$html = $page->htmlPage ();
			//返回搜索结果和分页信息
			return array ('list' => $list, 'html' => $html, 'total' => $page->allNums, 'eachNums' => $page->eachNums );
		}else {
			return $this->db->getRow($sql);
		}
		
	}
	
	function setUserData($data){
		$user= $_SESSION['user'];
		$uid = $user['uid'];
		foreach ($data as $k=>$v){			
			if (($user[$k]+$v)<0){
				return false;
			}			
		}
		foreach ($data as $k=>$v){
			$sql = "UPDATE user_data SET `$k`=`$k`+$v WHERE uid = $uid";
			$this->db->query($sql);
			$_SESSION['user'][$k]+=$v;
		}
		return true;
	}
	public static function getArardtype($id){
		switch ($id){
			case 0:
				return '充值奖励';
			case 1:
				return '推荐奖励';
			case 2:
				return '信息奖励';
			case 3:
				return '做任务奖励';
			case 4:
				return '签到奖励';
			case 5:
				return '抽奖';
			case 6:
				return '充值奖励';
		}
	}
	public function getAddress($uid){
	    $sql = "SELECT * FROM user_addr WHERE uid = $uid ORDER BY id DESC";
	    return $this->db->getAll($sql);
	}
	public function getBank($uid){
		$sql = "SELECT * FROM user_bank WHERE uid = $uid ORDER BY id DESC";
		return $this->db->getAll($sql);
	}
	public function getBankCount($uid){
		$sql = "SELECT COUNT(1) AS c FROM user_bank WHERE uid = $uid ";
		return $this->db->getOne($sql);	
	}
	public function getAddressCount($uid){
		$sql = "SELECT COUNT(1) AS c FROM user_addr WHERE uid = $uid ";
		return $this->db->getOne($sql);		
	}
}